#####################################################################
#                                                                   #
#  THIS IS A SOURCE CODE FILE FROM A PROGRAM TO INTERACT WITH THE   #
# LBRY PROTOCOL ( lbry.com ). IT WILL USE THE LBRY SDK ( lbrynet )  #
# FROM THEIR REPOSITORY ( https://github.com/lbryio/lbry-sdk )      #
# WHICH I GONNA PRESENT TO YOU AS A BINARY. SINCE I DID NOT DEVELOP #
# IT AND I'M LAZY TO INTEGRATE IN A MORE SMART WAY. THE SOURCE CODE #
# OF THE SDK IS AVAILABLE IN THE REPOSITORY MENTIONED ABOVE.        #
#                                                                   #
#      ALL THE CODE IN THIS REPOSITORY INCLUDING THIS FILE IS       #
# (C) J.Y.Amihud and Other Contributors 2021. EXCEPT THE LBRY SDK.  #
# YOU CAN USE THIS FILE AND ANY OTHER FILE IN THIS REPOSITORY UNDER #
# THE TERMS OF GNU GENERAL PUBLIC LICENSE VERSION 3 OR ANY LATER    #
# VERSION. TO FIND THE FULL TEXT OF THE LICENSE GO TO THE GNU.ORG   #
# WEBSITE AT ( https://www.gnu.org/licenses/gpl-3.0.html ).         #
#                                                                   #
# THE LBRY SDK IS UNFORTUNATELY UNDER THE MIT LICENSE. IF YOU ARE   #
# NOT INTENDING TO USE MY CODE AND JUST THE SDK. YOU CAN FIND IT ON #
# THEIR OFFICIAL REPOSITORY ABOVE. THEIR LICENSE CHOICE DOES NOT    #
# SPREAD ONTO THIS PROJECT. DON'T GET A FALSE ASSUMPTION THAT SINCE #
# THEY USE A PUSH-OVER LICENSE, I GONNA DO THE SAME. I'M NOT.       #
#                                                                   #
# THE LICENSE CHOSEN FOR THIS PROJECT WILL PROTECT THE 4 ESSENTIAL  #
# FREEDOMS OF THE USER FURTHER, BY NOT ALLOWING ANY WHO TO CHANGE   #
# THE LICENSE AT WILL. SO NO PROPRIETARY SOFTWARE DEVELOPER COULD   #
# TAKE THIS CODE AND MAKE THEIR USER-SUBJUGATING SOFTWARE FROM IT.  #
#                                                                   #
#####################################################################

# This is for features that are developed only for Odysee. Apart from
# livestreams which are in a dedicated file.

import os
import json
import urllib.request

# This will make odysee think that we are just a normal browser running windows ( lol )
# and connecting from Odysee.com
headers = {"Origin":"https://odysee.com",
           "Referer":"https://odysee.com/",
           "User-Agent":"Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0"}

def get_odysee_notifications(win):

    # This function will query the odysee api for notifications.

    auth = win.settings["auth_token"]
    if not auth:
        return False

    url = "https://api.odysee.com/notification/list?auth_token="+auth+"&is_app_readable=true&type=comment_replies%2Chyperchat_replies"

    req = urllib.request.Request(url, data=None, headers=headers)

    f = urllib.request.urlopen(req)
    data = json.loads(f.read().decode('utf-8'))
    if "data" in data:
        return data["data"]
    else:
        return False

def get_odysee_views(win, claim_id):

    # This function will return the views of a particular claim ID

    auth = win.settings["auth_token"]
    if not auth:
        return False
    
    url="https://api.odysee.com/file/view_count?auth_token="+auth+"&claim_id="+claim_id
    
    req = urllib.request.Request(url, data=None, headers=headers)

    f = urllib.request.urlopen(req)
    data = json.loads(f.read().decode('utf-8'))
    if "data" in data:
        return data["data"]
    else:
        return False

def get_odysee_subs(win, claim_id):

    # This function will return the views of a particular claim ID

    auth = win.settings["auth_token"]
    if not auth:
        return False
    
    url="https://api.odysee.com/subscription/sub_count?auth_token="+auth+"&claim_id="+claim_id
    
    req = urllib.request.Request(url, data=None, headers=headers)

    f = urllib.request.urlopen(req)
    data = json.loads(f.read().decode('utf-8'))
    if "data" in data:
        return data["data"]
    else:
        return False
